#!/bin/bash

# daemon 程序的名称
DAEMON_NAME="mindie_llm_back"

# 获取 daemon 程序的 PID
PID=$(ps aux | grep "$DAEMON_NAME" | grep -v grep | awk '{print $2}')

echo "$PID"

# 检查是否找到 PID
if [[ -z "$PID" ]]; then
  echo "Error: Daemon process not found."
fi

# 尝试发送 SIGTERM 信号
echo "Sending SIGTERM signal to daemon process (PID: $PID)..."
for pid in $PID; do
 kill "$pid"
done

# 等待一段时间，检查 daemon 程序是否已退出
sleep 5

# 再次检查 PID
PID=$(ps aux | grep "$DAEMON_NAME" | grep -v grep | awk '{print $2}')

# 如果 daemon 程序仍然存在，发送 SIGKILL 信号
if [[ -n "$PID" ]]; then
  echo "Daemon process still running. Sending SIGKILL signal..."
  for pid in $PID; do
     kill -9 "$pid"
  done
fi

pkill -9 mind

echo "Daemon process killed."

exit 0
